package com.yang.zngcxyxkjs.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class FileController {

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            // 检查文件是否为空
            if (file.isEmpty()) {
                return ResponseEntity.badRequest().body("请选择一个文件进行上传");
            }

            // 获取文件名
            String fileName = file.getOriginalFilename();

            // 指定文件存储路径
            String uploadDir = "E:\\SSM\\zngcxyxkjs\\src\\main\\resources\\file\\" + fileName;

            // 将文件保存到服务器指定路径
            file.transferTo(new File(uploadDir));

            return ResponseEntity.ok("文件上传成功,路径为:" + uploadDir);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败");
        }
    }

}
